/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.uhk.ppro.traveler.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

/**
 *
 * @author dusi
 */
@Entity
public class TBudget implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private float minBudget;
    private float income;
    private float expense;
    private float defaultState;
    @Transient
    private float total;

    // Relationships
    @OneToOne(mappedBy="budget")
    private TUser user;
    @OneToMany(mappedBy="budget", cascade=CascadeType.ALL)
    private List<TTransaction> transactions = new ArrayList<TTransaction>();

    // Constructor
    public TBudget() {

        this.minBudget = 0;
    }

    public TBudget(float minBudget, float income, float expense, float defaultState, TUser user) {
        
        this.minBudget = minBudget;
        this.income = income;
        this.expense = expense;
        this.defaultState = defaultState;
        this.user = user;
    }
    
    // Actions
    public void calculateTotal(){
        
        float in = 0;
        float ex = 0;
        
        for (TTransaction t : transactions) {
            
            if(t.getType() == 1) {
                
                in += t.getAmount();
            }
                    
            else {
                
                ex += t.getAmount();
            }
        }
        
        this.setTotal(getDefaultState() + in - ex);
    }
    
    // Accessors
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public float getMinBudget() {
        return minBudget;
    }

    public void setMinBudget(float minBudget) {
        this.minBudget = minBudget;
    }

    public TUser getUser() {
        return user;
    }

    public void setUser(TUser user) {
        this.user = user;
    }

    public List<TTransaction> getTransactions() {
        return transactions;
    }

    public void setTransactions(List<TTransaction> transactions) {
        this.transactions = transactions;
    }

    public float getIncome() {
        return income;
    }

    public void setIncome(float income) {
        this.income = income;
    }

    public float getExpense() {
        return expense;
    }

    public void setExpense(float expense) {
        this.expense = expense;
    }

    public float getDefaultState() {
        return defaultState;
    }

    public void setState(float defaultState) {
        this.defaultState = defaultState;
    }

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }
    
    // General
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TBudget)) {
            return false;
        }
        TBudget other = (TBudget) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "cz.uhk.ppro.traveler.entity.TBudget[id=" + id + "]";
    }

}
